from rest_framework import serializers
from UserProfile.models import User
from datetime import datetime
from  Blog.models import Collection,Type
##继承序列化类
from czllblog import settings

class UserSerializer(serializers.ModelSerializer):
    avatar = serializers.SerializerMethodField()  # 头像
    email_status =  serializers.SerializerMethodField()#邮箱状态
    gender = serializers.SerializerMethodField() #性别
    class Meta:
        model = User
        #序列化所有字段
        fields = ('id','nickname', 'username','realname','tel','avatar','gender','email','email_status','birth','introduction')

    # 头像链接拼接
    def get_avatar(self, obj):
        rel_path = obj['avatar']
        rely_on_path = settings.MEDIA_URL
        return rely_on_path + rel_path

    # 邮箱状态
    def get_email_status(self, obj):
        rel = obj['email_status']
        if rel is 0:
            value = "未绑定"
        else:
            value = "已绑定"
        return value

    #性别
    def get_gender(self,obj):
        rel = obj['gender']
        if rel is 1:
            value = "男"
        elif rel is 2:
            value = "女"
        else:
            value = None
        return value


class CollectionsSerializer(serializers.ModelSerializer):
    blog_id = serializers.SerializerMethodField()
    blog_cover = serializers.SerializerMethodField()#封面
    blog_ctime = serializers.SerializerMethodField()#创建时间
    #类型
    blog_type = serializers.SerializerMethodField()
    blog_type_name = serializers.SerializerMethodField()
    #用户
    blog_userinfo = serializers.SerializerMethodField()
    id = serializers.IntegerField()
    ctime = serializers.SerializerMethodField()

    blog_title = serializers.SerializerMethodField()
    blog_is_recommend = serializers.SerializerMethodField()
    blog_view = serializers.SerializerMethodField()
    blog_comments = serializers.SerializerMethodField()
    blog_goods = serializers.SerializerMethodField()
    blog_collections = serializers.SerializerMethodField()
    blog_content = serializers.SerializerMethodField()

    class Meta:
        model = Collection
        #序列化所有字段
        fields = ('id','ctime','blog_id','blog_type','blog_type_name','blog_userinfo','blog_cover','blog_title','blog_is_recommend','blog_ctime','blog_view','blog_comments','blog_goods','blog_collections','blog_content')


    def get_blog_is_recommend(self,obj):
        blog_is_recommend = obj['blog__is_recommend']
        return blog_is_recommend

    def get_blog_title(self,obj):
        blog_title = obj['blog__title']
        return blog_title

    def get_blog_view(self,obj):
        blog_view = obj['blog__view']
        return blog_view

    def get_blog_comments(self,obj):
        blog_comments = obj['blog__comments']
        return blog_comments

    def get_blog_goods(self,obj):
        blog_goods = obj['blog__goods']
        return blog_goods

    def get_blog_collections(self,obj):
        blog_collections = obj['blog__collections']
        return blog_collections

    def get_blog_content(self,obj):
        blog_content = obj['blog__content']
        return blog_content

    def get_blog_id(self,obj):
        blog_id = obj['blog__id']
        return blog_id

    def get_blog_type(self,obj):
        blog_type = obj['blog__type']
        return blog_type

    #图片链接拼接
    def get_blog_cover(self, obj):
        rel_path = obj['blog__cover']
        rely_on_path = settings.MEDIA_URL
        return rely_on_path + rel_path
    #处理时间
    def get_blog_ctime(self,obj):
        old_ctime = obj['blog__ctime']
        ctime = old_ctime.strftime('%Y-%m-%d %H:%M:%S')
        return ctime
        # 处理时间

    def get_ctime(self, obj):
        old_ctime = obj['ctime']
        ctime = old_ctime.strftime('%Y-%m-%d %H:%M:%S')
        return ctime

    #获取外键类型信息
    def get_blog_type_name(self,obj):
        type = obj['blog__type']
        type_res = Type.objects.filter(id=type).values('name')[:1][0]
        # print()
        return type_res['name']
    def get_blog_userinfo(self,obj):
        user = obj['blog__user']
        user_res = User.objects.filter(id=user).values('id','username','nickname','avatar')[:1][0]
        user_res['avatar'] = settings.MEDIA_URL + user_res['avatar']
        return user_res